package com.gdpa.controller;


import com.gdpa.entity.DistrArea;
import com.gdpa.entity.DistrBuilding;
import com.gdpa.service.IDistrAreaService;
import com.gdpa.service.IDistrBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 分销楼盘表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/distr-building")
public class DistrBuildingController {
    @Autowired
    IDistrBuildingService buildingService;

    @RequestMapping("/toBuilding")
    public ModelAndView toBuilding(String data,HttpServletRequest request){
        System.out.println("收到数据："+data);
        DistrArea area = buildingService.AreaIsExist(data);
        request.getSession().setAttribute("area",area);
        if (area != null){
            List<DistrBuilding> list = buildingService.getAllBuilding(area.getDistrAreaId());
            if (list != null){
                request.setAttribute("list",list);
            }
            request.setAttribute("area",area);
        }
        ModelAndView mv = new ModelAndView("distr_building/building_manage");
        return mv;
    }

    @RequestMapping("/findBuild")
    public ModelAndView findBuild(HttpServletRequest request){
        DistrBuilding building = requestBuild(request);
        System.out.println("find收到！！！"+building);
        if (building != null){
            DistrArea area = buildingService.AreaIsExist(building.getDistrAreaId().toString());
            List<DistrBuilding> list = buildingService.findBuilding(building);
            if (list != null && area != null){
                request.setAttribute("area",area);
                request.setAttribute("list",list);
            }
        }
        ModelAndView mv = new ModelAndView("distr_building/building_manage");
        return mv;
    }

    @RequestMapping("/addBuild")
    public ModelAndView addBuild(HttpServletRequest request){
        DistrBuilding building = requestBuild(request);
        System.out.println("add收到！！！"+building);
        if (building != null){
            buildingService.addBuild(building);
        }
        ModelAndView mv = new ModelAndView("redirect:toBuilding?data="+building.getDistrAreaId());
        return mv;
    }

    @RequestMapping("/deleteBuild")
    public ModelAndView deleteBuild(String data, HttpServletRequest request){
        buildingService.deleteBuild(data);
        DistrArea area = (DistrArea) request.getSession().getAttribute("area");
        ModelAndView mv = new ModelAndView("redirect:toBuilding?data="+area.getDistrAreaId());
        return mv;
    }

    public DistrBuilding requestBuild(HttpServletRequest request){
        DistrBuilding building = new DistrBuilding();
        String distrBuildingName = request.getParameter("distrBuildingName");
        String traffic = request.getParameter("traffic");
        String property = request.getParameter("property");
        String qualityIndex = request.getParameter("qualityIndex");
        String deliveryTime = request.getParameter("deliveryTime");
        String customer = request.getParameter("customer");
        String distrAreaId =request.getParameter("distrAreaId");


        building.setDistrBuildingName(distrBuildingName);

        building.setTraffic(Integer.parseInt(traffic.equals("")?"0":traffic));


        building.setProperty(property);

        building.setQualityIndex(qualityIndex);

        building.setDeliveryTime(deliveryTime);

        building.setCustomer(customer);

        building.setDistrAreaId(Integer.parseInt(distrAreaId.trim()));

        return building;
    }
}
